local CPloot = LibStub("AceAddon-3.0"):GetAddon("CPloot")
CPlootFrames = {}   -- DEBUG Should be local
CPlootFrameObj = {}  -- DEBUG Should be local
local DropDownTable = {} 
local CPlootFramesRuntime = {
	CPlootTextBoxWidth = nil, -- Used to calculate offset for MainSpec Button
	CPlootResultFrameBaseHeight = 160,
}


function CPlootFrames:OnInitialize()
	CPlootFrames:CreateBackgroundFrame("CPlootFrame")
	CPlootFrames:CreateBackgroundFrame("CPlootResultFrame")
	CPlootFrames:CreateBackgroundFrame("CPlootUpdateWarning")
	CPlootFrames:CreateBackgroundFrame("CPlootChooseML")
	CPlootFrames:CreateOwnTooltip()
	CPlootFrames:CreateResultPlayerReasignDDFrame()
end

function CPlootFrames:OnEnable()
	UIDropDownMenu_Initialize(CPlootFrames:GetFrame("CPlootResultPlayerDDFrame"), CPlootInitPlayerDDFrame, "MENU")

end

----------------------------------------------------
----          Main background windows           ----
----------------------------------------------------
function CPlootFrames:CreateBackgroundFrame(FrameName)
   if (not FrameName) then
	  CPloot:Print("No Frame Name in CreateBackgroundFrame")
	  return 
   end
   
   local CPlootBGMainFrame = { 
	  tile = true, 
	  tileSize = 32, 
	  edgeSize = 32, 
	  insets = {left = 11, right = 12, top = 12, bottom = 11}, 
	  bgFile = "Interface/DialogFrame/UI-DialogBox-Background", 
	  edgeFile = "Interface/DialogFrame/UI-DialogBox-Border",
   }

   local CPlootBGTitleFrame = {
	   bgFile = "Interface/DialogFrame/UI-DialogBox-Header",
   }

   local Frame = CreateFrame("Frame", FrameName, UIParent) 
   	
   if (CPloot_ConfigDb[FrameName] == nil) then
	  CPloot_ConfigDb[FrameName] = {
		 Point = "CENTER",
		 X = 0,
		 Y = 0,
	  }
   end
   
   Frame:SetPoint(CPloot_ConfigDb[FrameName]["Point"], UIParent, CPloot_ConfigDb[FrameName]["Point"], CPloot_ConfigDb[FrameName]["X"] , CPloot_ConfigDb[FrameName]["Y"]) 
   Frame:SetHeight(250) 
   Frame:SetBackdrop(CPlootBGMainFrame)
   Frame:SetFrameStrata("FULLSCREEN_DIALOG")
   Frame:SetBackdropColor(0,0,0,1)
   Frame:SetBackdropBorderColor(179,132,5,1)
   Frame:SetMovable(true)
   Frame:EnableMouse(true)
   Frame:RegisterForDrag("LeftButton")
   Frame:SetScript("OnDragStart", function(self) if self:IsMovable() then self:StartMoving() end end)
   Frame:SetScript("OnDragStop", function(...) CPlootFrames:SetBGFramePoints(...) end)
 
   -- Create the Title Frame
   local Title = CreateFrame("Frame", FrameName .. "Title", Frame)
   Title:SetBackdrop(CPlootBGTitleFrame)
   Title:SetPoint("TOP", Frame, "TOP", 0, 13.5)
   Title:SetHeight(64)
   Title:SetWidth(256)
   Title.Text = Title:CreateFontString(FrameName .. "TitleText", "ARTWORK", "GameFontNormal")
   Title.Text:SetPoint("CENTER", Title, "CENTER", 0, 11)
   Title.Text:SetText("CPloot")
   Frame.Title = Title

   -- Create the Close button for LootFrame
   local CloseButton = CreateFrame("Button", FrameName .. "CloseButton", Frame, "UIPanelCloseButton")
   CloseButton:SetPoint("TOPRIGHT", FrameName, "TOPRIGHT", -6, -6)
   CloseButton:SetScript("OnMouseDown", function() end)

   -- Create the help button
   local HelpButton = CreateFrame("Button", FrameName .. "HelpButton", Frame, "UIPanelButtonTemplate")
   HelpButton:SetPoint("TOPRIGHT", CloseButton, "TOPLEFT", 0, -7)
   HelpButton:SetText("?")
   HelpButton:SetHeight(18)
   HelpButton:SetWidth(15)
   HelpButton:SetScript("OnEnter", function() CPlootFrames:CreateHelpTooltip(HelpButton) end)
   HelpButton:SetScript("OnLeave", function() GameTooltip:Hide() end)
   CPlootFrameObj[FrameName .. "HelpButton"] = HelpButton

   CPlootFrameObj[FrameName] = Frame

   if (FrameName == "CPlootFrame") then  -- CPlootFrame only stuff
	 CloseButton:SetScript("OnMouseUP", CPloot.CloseLootFrame)
	 Frame:SetWidth(400)
	 Title.Text:SetText("CPloot")
	 CPlootFrames:CreateStatusFrame()
	 CPlootFrames:UpdateStatusFrame()
	 CPlootFrames:CreateAnnounceButton()
	 CPlootFrames:CreateCPlootWinnerListDateFrame()
  elseif (FrameName == "CPlootResultFrame") then  -- ResultFrame only stuff
	 CloseButton:SetScript("OnMouseUP", CPloot.CloseResultFrame)
	 Frame:SetWidth(560)  -- Set the initial width of the CPlootResultFrame
	 Frame:SetHeight(CPlootFramesRuntime["CPlootResultFrameBaseHeight"])
	 Title.Text:SetText("Roll Window")	 CPlootFrames:CreateResultWindowFrames() -- Create the Raider/member/unknown colum and Titles
	 CPlootFrames:CreateNukeButton()
 elseif (FrameName == "CPlootUpdateWarning") then
	 Frame:SetWidth(450)
	 Frame:SetHeight(200)
	 Title.Text:SetText("CPloot")
	 CPlootFrames:SetCPlootUpdateWarning(Frame)
 elseif (FrameName == "CPlootChooseML") then
	 Frame:SetWidth(160)
	 Frame:SetHeight(250)
	 Title.Text:SetText("Choose ML")
	 HelpButton:Hide()
	 CloseButton:Hide()
 end
 
	 Frame:Hide()

end

----------------------------------------------------
---- Save the relative X Y coordinates for      ----
---- SetPoint to the CPloot_ConfigDb           ----
----------------------------------------------------
function CPlootFrames:SetBGFramePoints(this)
   if this:IsMovable() then
	  this:StopMovingOrSizing()
   end

   local point, _, _, xOfs, yOfs = this:GetPoint()
   CPloot_ConfigDb[this:GetName()] = {
	  Point = point,
	  X = xOfs,
	  Y = yOfs,
   }
end

----------------------------------------------------
----      Create the hidden tooltips            ----
----------------------------------------------------
function CPlootFrames:CreateOwnTooltip()
	local CPlootTooltip = CreateFrame("GameTooltip", "CPlootTooltip", UIParent, "GameTooltipTemplate")
	CPlootTooltip:Hide()
	CPlootFrameObj["CPlootTooltip"] = CPlootTooltip
end

----------------------------------------------------
----         Crete the Help tooltips            ----
----------------------------------------------------
function CPlootFrames:CreateHelpTooltip(Frame)
	GameTooltip:SetOwner(Frame, "ANCHOR_LEFT")

	if (Frame:GetName() == "CPlootFrameHelpButton") then
		GameTooltip:AddLine("Top Left date", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("This is the date your last reset the winnerlist")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Offical / Unofficial Frame", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("While running a raid you can at any time see what")
		GameTooltip:AddLine("mode the addon is in. To change mode simply click it")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Green background saying Official is offical raids")
		GameTooltip:AddLine("Official raids on mainspec rolls look at winnerlist,")
		GameTooltip:AddLine("rank and roll")
		GameTooltip:AddLine("Official raids on offspec rolls only look at rank")
		GameTooltip:AddLine("and roll")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Red background saying Unofficial is unofficial raids")
		GameTooltip:AddLine("Unofficial raids only look at the winnerlist for both")
		GameTooltip:AddLine("mainspec and offspec (shared winnerlist). All rollers")
		GameTooltip:AddLine("will be seen as raiders")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Item color codes", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("The color codes is only a guide and can be wrong")
		GameTooltip:AddLine("use your own judgement too")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("The priority is Red, Blue, Green, rest")
		GameTooltip:AddLine("Red = Weapon")
		GameTooltip:AddLine("Blue = Tier item")
		GameTooltip:AddLine("Green = Trinket / rings / held in Off-Hand / Neck")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine(" ")

	elseif (Frame:GetName() == "CPlootResultFrameHelpButton") then
	-- Colors are r g b in 0 - 1 format so take RGB color and devide with 255
		GameTooltip:AddLine("The player lines consists of ", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("Roll  |  PlayerName  |  Position in the winnerlist")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Roll", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("This is the actual roll made by the player")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("PlayerName", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("The name of the player color coded by class") 
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("WinnerList", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("The number that player has in the winnerlist.") 
		GameTooltip:AddLine("When some one wins an item they go to the")
		GameTooltip:AddLine("end of the list (higest number)")
		GameTooltip:AddLine("The winnerlist should only be looked at")
		GameTooltip:AddLine("if you have a roller in the unknown section,")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("The person with the lowest winnerlist number,")
		GameTooltip:AddLine("should win if they have the same priority (rank).")
		GameTooltip:AddLine("This could be a raiders alt who is not in the guild.")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("If a person has not won an item, no number will be")
		GameTooltip:AddLine("displayed to the right of the name")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Who should have the item", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("Start in the top left corner and move to")
		GameTooltip:AddLine("the bottom right corner. If there is any")
		GameTooltip:AddLine("one in the unknown section, you must go")
		GameTooltip:AddLine("by the rules agreed on")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("Unknown colum", 102/255, 255/255, 255/255)
		GameTooltip:AddLine("The Unknown colum will only show if there is")
		GameTooltip:AddLine("any one to display in that colum.")
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine(" ")
	end
	GameTooltip:Show()
end

----------------------------------------------------
----  Crete the CPlootFrameStatus frame        ----
----------------------------------------------------
function CPlootFrames:CreateStatusFrame()

	local CPlootFrameBG = {
		tile = true,
		tileSize = 8,
		edgeSize = 8,
		insets = {left = 1, right = 1, top = 1, bottom = 1},
		bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
	}

	local Frame = CreateFrame("Frame", "CPlootFrameStatus", CPlootFrame)
	Frame:SetBackdrop(CPlootFrameBG)
	Frame:SetBackdropColor(1,1,1,0.5) 
	Frame:SetPoint("TOPLEFT","CPlootFrame","TOPLEFT", 30, -55)
	Frame:SetPoint("TOPRIGHT", "CPlootFrame","TOPRIGHT", -30, -55)
	Frame:SetBackdropBorderColor(35,135,70,1)
	Frame.Text = Frame:CreateFontString("CPlootFrameStatusText", "ARTWORK", "GameFontNormal") 
	Frame.Text:SetText( CPloot:GetAddonModeText() )
	Frame.Text:SetPoint("CENTER", Frame, "CENTER", 0,0)
	Frame:SetHeight(25) 
	Frame:EnableMouse(true)
	Frame:SetScript("OnMouseDown", function()  end)
	Frame:SetScript("OnMouseUP", CPloot.ChangeMode)
	CPlootFrameObj["CPlootFrameStatus"] = Frame
end

----------------------------------------------------
----          Update warning window             ----
----------------------------------------------------
function CPlootFrames:SetCPlootUpdateWarning(Frame)
	local CPlootFrameBG = {
		tile = true,
		tileSize = 8,
		edgeSize = 12,
		insets = {left = 2, right = 2, top = 2, bottom = 2},
		bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
	}

	local CPlootUpdateInfo = CreateFrame("Frame", "CPlootUpdateInfo", Frame)
	CPlootUpdateInfo:SetBackdrop(CPlootFrameBG)
	CPlootUpdateInfo:SetBackdropColor(0/255,0/255,0/255,0.6)
	CPlootUpdateInfo:SetBackdropBorderColor(255/255,153/255,0/255,1)
	CPlootUpdateInfo:SetPoint("TOP","CPlootUpdateWarning","TOP", 0, -40)
	CPlootUpdateInfo.Text = CPlootUpdateInfo:CreateFontString("CPlootUpdateInfoText", "ARTWORK", "GameFontNormal")
	CPlootUpdateInfo.Text:SetText("CPloot is out of date, a new version is available, please update right away.\n\n Use CPlootTransporter to update the addon\n\n or Click in the box below and press\n CTRL + a \n CTRL + c \n to copy the download link")
	CPlootUpdateInfo.Text:SetAllPoints(CPlootUpdateInfo)
	CPlootUpdateInfo:SetHeight(118)
	CPlootUpdateInfo:SetWidth(400)

	local CPlootUpdateEditBox = CreateFrame("EditBox", "CPlootUpdateEditBox", Frame, "InputBoxTemplate")
	CPlootUpdateEditBox:SetWidth(400)
	CPlootUpdateEditBox:SetHeight(20)
	CPlootUpdateEditBox:SetPoint("BOTTOM", Frame, "BOTTOM", 0, 20)
	CPlootUpdateEditBox:SetAutoFocus(false)
	CPlootUpdateEditBox:SetText(CPlootRuntime["DownloadURL"])
	CPlootFrameObj["CPlootUpdateEditBox"] = CPlootUpdateEditBox

end

----------------------------------------------------
----         Create the announce button         ----
----------------------------------------------------
function CPlootFrames:CreateAnnounceButton()

   local AnnounceButton = CreateFrame("Button", "CPlootAnnounceButton", CPlootFrames:GetFrame("CPlootFrame"), "UIPanelButtonTemplate")
   AnnounceButton:SetPoint("BOTTOMLEFT", "CPlootFrameStatus", "TOPLEFT", 0, 5)
   AnnounceButton:SetText("Announce")
   AnnounceButton:SetWidth(100)
   AnnounceButton:SetHeight(25)
   AnnounceButton:SetScript("OnMouseUp", function() CPloot:AnnounceLoot() end)
   AnnounceButton:SetScript("OnMouseDown", function()  end)
   CPlootFrameObj["CPlootAnnounceButton"] = AnnounceButton

end

----------------------------------------------------
----            Update Status frame             ----
----------------------------------------------------
function CPlootFrames:UpdateStatusFrame()
	if (CPloot_ConfigDb["OfficialMode"]) then
		CPlootFrameObj["CPlootFrameStatus"]:SetBackdropColor(0/255, 153/255, 0/255, 0.5);
	else
		CPlootFrameObj["CPlootFrameStatus"]:SetBackdropColor(153/255, 0/255, 0/255, 0.5);
	end
	CPlootFrameObj["CPlootFrameStatus"]["Text"]:SetText( CPloot:GetAddonModeText() )
end

----------------------------------------------------
----      Create the winnerlist reset frame     ----
----------------------------------------------------
function CPlootFrames:CreateCPlootWinnerListDateFrame()
	local Frame = CreateFrame("Frame","CPlootlootWinnerListDateFrame", CPlootFrameObj["CPlootFrame"])
	Frame:SetPoint("TOPLEFT", "CPlootFrame", "TOPLEFT", 12, -12)
	Frame.Text = Frame:CreateFontString("LootListDateFrameText", "ARTWORK", "GameFontNormalSmall")
	Frame.Text:SetPoint("CENTER", Frame, "CENTER", 0,0)
	Frame.Text:SetText(date("%m/%d/%y %H:%M"))
	Frame:SetHeight(Frame.Text:GetHeight())
	Frame:SetWidth(Frame.Text:GetWidth())
	CPlootFrameObj["CPlootlootWinnerListDateFrame"] = Frame
end

----------------------------------------------------
----  Crete the loot rows in the CPlootFrame   ----
----------------------------------------------------
function CPlootFrames:CreateLootRow(LootInfo)

	local Frame = CPlootFrames:GetFrame("CPlootTextBox" .. LootInfo["ButtonId"])
	if (not Frame) then

		-- Create Text frame for itemlink text
		local CPlootFrameBG = {
			tile = true,
			tileSize = 8,
			edgeSize = 12,
			insets = {left = 2, right = 2, top = 2, bottom = 2},
			bgFile = "Interface/Tooltips/UI-Tooltip-Background",
			edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
		}

		-- Create the Loot Text box
		local Frame = CreateFrame("Frame", "CPlootTextBox" .. LootInfo["ButtonId"], CPlootFrame)
		Frame:SetBackdrop(CPlootFrameBG)
		Frame:SetBackdropColor(0,0,0,0.8) 
		if (LootInfo["ButtonId"] == 1) then
			Frame:SetPoint("TOPLEFT","CPlootFrameStatus","BOTTOMLEFT", 0, -7)
		else
			local Anchor = LootInfo["ButtonId"] -1
			Frame:SetPoint("TOPLEFT","CPlootTextBox" .. Anchor,"BOTTOMLEFT", 0, -7)
		end
		Frame:SetBackdropBorderColor(255/255,0,0,1)
		Frame.Text = Frame:CreateFontString("CPlootTextBoxText".. LootInfo["ButtonId"], "ARTWORK", "GameFontNormal") 
		Frame.Text:SetText(LootInfo["ItemLink"])
		Frame.Text:SetPoint("CENTER", Frame, "CENTER", 0,0)
		Frame:SetHeight(Frame.Text:GetHeight() + 16) 
		Frame:SetWidth(Frame.Text:GetWidth() +17) 
		Frame:EnableMouse(true)
		Frame:SetScript("OnEnter", function() GameTooltip:SetOwner(Frame,"ANCHOR_CURSOR") GameTooltip:SetHyperlink(LootInfo["ItemLink"]) end)
		Frame:SetScript("OnLeave", function() GameTooltip:Hide() end)
		Frame:SetID(LootInfo["LootSlotId"])
		CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]] = Frame

		-- Create the MainSpec button
		local MainSpec = CreateFrame("Button","CPlootMainSpec" .. LootInfo["ButtonId"], Frame, "UIPanelButtonTemplate")

		if (LootInfo["ButtonId"] == 1) then
			MainSpec:SetPoint("TOPLEFT", "CPlootFrameStatus", "TOPLEFT", 12, -4)
		else
			local Anchor = LootInfo["ButtonId"] -1
			MainSpec:SetPoint("TOPLEFT", "CPlootMainSpec" .. Anchor, "BOTTOMLEFT", 0, -15)
		end
		MainSpec:SetText("MainSpec")
		MainSpec:SetScript("OnMouseUP", function(...) CPloot:StartRolls(...) end)
		MainSpec:SetScript("OnMouseDown", function() end)
		MainSpec:SetWidth(MainSpec:GetTextWidth() + 16)
		MainSpec:SetHeight(MainSpec:GetTextHeight() + 8)
		MainSpec:SetID(LootInfo["LootSlotId"])
		MainSpec["LootInfo"] = LootInfo
		CPlootFrameObj["CPlootMainSpec" .. LootInfo["ButtonId"]] = MainSpec

		-- Create the OffSpec button
		local OffSpec = CreateFrame("Button","CPlootOffSpec" ..  LootInfo["ButtonId"], Frame, "UIPanelButtonTemplate")
		OffSpec:SetPoint("TOPLEFT", MainSpec, "TOPRIGHT", 5, 0)
		OffSpec:SetText("OffSpec")
		OffSpec:SetScript("OnMouseUP", function(...) CPloot:StartRolls(...) end)
		OffSpec:SetScript("OnMouseDown", function() end)
		OffSpec:SetWidth(OffSpec:GetTextWidth() + 16)
		OffSpec:SetHeight(OffSpec:GetTextHeight() + 8)
		OffSpec:SetID(LootInfo["LootSlotId"])
		OffSpec["LootInfo"] = LootInfo

		if (LootInfo["BindType"] and LootInfo["BindType"] == "BoE" and CPloot_ConfigDb["OfficialMode"]) then	-- Never do Offspec rolls on BoE items.
			OffSpec:Hide()
		else
			OffSpec:Show()
		end

		-- Overwrite BindType if it is a Recipe.
		if (LootInfo["ItemType"] and LootInfo["ItemType"] == "Recipe" and CPloot_ConfigDb["OfficialMode"]) then -- Overwrite BindType if Recipe
			OffSpec:Show()
			MainSpec:Hide()
		end

		CPlootFrameObj["CPlootOffSpec" ..  LootInfo["ButtonId"]] = OffSpec

	else -- We have an existing frame
		local Frame = CPlootFrames:GetFrame("CPlootTextBox" ..  LootInfo["ButtonId"])
		Frame.Text:SetText(LootInfo["ItemLink"])
		Frame:SetHeight(Frame.Text:GetHeight() + 16)
		Frame:SetWidth(Frame.Text:GetWidth() +12)
		Frame:SetScript("OnEnter", function() GameTooltip:SetOwner(Frame,"ANCHOR_CURSOR") GameTooltip:SetHyperlink(LootInfo["ItemLink"]) end)
		Frame:SetScript("OnLeave", function() GameTooltip:Hide() end)
		Frame:Show()

		-- Reuse the MainSpec button
		local MainSpec = CPlootFrames:GetFrame("CPlootMainSpec".. LootInfo["ButtonId"])
		MainSpec:SetText("MainSpec")
		MainSpec:SetScript("OnMouseUP", function(...) CPloot:StartRolls(...) end)
		MainSpec:SetWidth(MainSpec:GetTextWidth() + 16)
		MainSpec:SetHeight(MainSpec:GetTextHeight() + 8)
		MainSpec["LootInfo"] = LootInfo
		MainSpec:Show()

		-- Reuse the OffSpec button
		local OffSpec = CPlootFrames:GetFrame("CPlootOffSpec" .. LootInfo["ButtonId"])
		OffSpec:SetText("OffSpec")
		OffSpec:SetScript("OnMouseUP", function(...) CPloot:StartRolls(...) end)
		OffSpec:SetWidth(OffSpec:GetTextWidth() + 16)
		OffSpec:SetHeight(OffSpec:GetTextHeight() + 8)
		OffSpec["LootInfo"] = LootInfo
		if (LootInfo["BindType"] and LootInfo["BindType"] == "BoE" and CPloot_ConfigDb["OfficialMode"]) then	-- Never do Offspec rolls on BoE items.
			OffSpec:Hide()
		else
			OffSpec:Show()
		end

		-- Overwrite BindType if it is a Recipe.
		if (LootInfo["ItemType"] and LootInfo["ItemType"] == "Recipe" and CPloot_ConfigDb["OfficialMode"]) then -- Overwrite BindType if Recipe
			OffSpec:Show()
			MainSpec:Hide()
		end

	end

	-- Set CPlootFrame height
	local LootFrameHeight = ((CPlootFrameObj["CPlootTextBox1"]:GetHeight() + 10) * LootInfo["ButtonId"]) + 90
	CPlootFrameObj["CPlootFrame"]:SetHeight(LootFrameHeight)

	-- Get the CPlootTextBox
	for i=1, LootInfo["ButtonId"] do
		if (CPlootFramesRuntime["CPlootTextBoxWidth"]) then  -- Get the widest frames pixels
			if (CPlootFrameObj["CPlootTextBox" .. i]:GetWidth() > CPlootFramesRuntime["CPlootTextBoxWidth"]) then
				CPlootFramesRuntime["CPlootTextBoxWidth"] = CPlootFrameObj["CPlootTextBox" .. i]:GetWidth()
			end
		else
			CPlootFramesRuntime["CPlootTextBoxWidth"] = CPlootFrameObj["CPlootTextBox" .. i]:GetWidth()
		end
	end

	-- Set the last reset date of the Winnerlist
	CPlootFrameObj["CPlootlootWinnerListDateFrame"]["Text"]:SetText(CPloot_ConfigDb["WinnerListResetDate"])

	-- Set the Border corlor of the item text frame
	-- Set default color white
	CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropBorderColor(1,1,1,1)
	CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropColor(0,0,0,0.8) 

	-- Weapons	Red
	if ( LootInfo["ItemType"] == "Weapon" ) then																	-- Weapons
		if not ( LootInfo["ItemSubType"] == "Wands") then
			CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropColor(255/255,0,0,0.4)					
			CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropBorderColor(255/255,0,0,1)
		end
		-- Armor Green
	elseif ( LootInfo["ItemType"] == "Armor") then																	-- Armor
		-- Miscellaneous = rings and trinkets
		if not ( LootInfo["ItemSubType"] == "Miscellaneous" or LootInfo["ItemSubType"] == "Librams" or LootInfo["ItemSubType"] == "Idols" or LootInfo["ItemSubType"] == " Totems") then
			CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropColor(0,255/255,0,0.4)
			CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropBorderColor(0,255/255,0,1)
		end
	end

	-- Tier items Blue
	if ( string.match(LootInfo["LootList"], "Tier.*") ) then
		CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropColor(0,128/255,255/255,0.4)
		CPlootFrameObj["CPlootTextBox" .. LootInfo["ButtonId"]]:SetBackdropBorderColor(0,128/255,255/255,1)
	end


	CPlootFrameObj["CPlootMainSpec1"]:SetPoint("TOPLEFT", "CPlootTextBox1", "TOPLEFT", CPlootFramesRuntime["CPlootTextBoxWidth"] + 10, -3)
	local LootFrameWidth = CPlootFramesRuntime["CPlootTextBoxWidth"] + CPlootFrameObj["CPlootMainSpec1"]:GetWidth() + CPlootFrameObj["CPlootOffSpec1"]:GetWidth()  
	CPlootFrameObj["CPlootFrame"]:SetWidth(LootFrameWidth + 80)
	CPlootFrameObj["CPlootFrame"]:Show()	
end


--------------------------------------------------------
----     Create the Nuke Button for ResultFrame     ----
--------------------------------------------------------
function CPlootFrames:CreateNukeButton()

	local NukeButton = CreateFrame("Button","CPlootNukeButton", CPlootFrameObj["CPlootResultFrame"], "UIPanelButtonTemplate")
	NukeButton:SetPoint("TOPRIGHT", CPlootFrameObj["CPlootResultFrameHelpButton"], "TOPLEFT", -7, 5)
	NukeButton:SetText("Nuke")
	NukeButton:SetScript("OnMouseUP", CPloot.NukeItem)
	NukeButton:SetScript("OnMouseDown", function() end)
	NukeButton:SetWidth(60)
	NukeButton:SetHeight(30)
	CPlootFrameObj["CPlootNukeButton"] = NukeButton

end

--------------------------------------------------------
----  Create the rows for raider / member / unknown ----
--------------------------------------------------------
function CPlootFrames:CreateResultWindowFrames()

	local CPlootFrameBGWhite = {
		tile = true,
		tileSize = 8,
		edgeSize = 12,
		insets = {left = 2, right = 2, top = 2, bottom = 2},
		bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
	}

   local CPlootFrameTitle = { 
	  tile = true, 
	  tileSize = 32, 
	  edgeSize = 32, 
	  insets = {left = 11, right = 12, top = 12, bottom = 11}, 
	  bgFile = "Interface/DialogFrame/UI-DialogBox-Background",
	  edgeFile = "Interface/DialogFrame/UI-DialogBox-Border",
   }

	-- Create Text frame for itemlink text
	local CPlootFrameBG = {
		tile = true,
		tileSize = 8,
		edgeSize = 12,
		insets = {left = 2, right = 2, top = 2, bottom = 2},
		bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
	}

	-- Create Info box
	local RollType = CreateFrame("Frame", "CPlootResultFrameRollType", CPlootResultFrame)
	RollType:SetBackdrop(CPlootFrameBG)
	RollType:SetBackdropColor(0/255,0/255,0/255,0.6) 
	RollType:SetBackdropBorderColor(255/255,153/255,0/255,1)
	RollType:SetPoint("TOP","CPlootResultFrame","TOP", 0, -33)
--	RollType:SetWidth(CPlootFramesRuntime["CPlootTextBoxWidth"] - 60)
	RollType.Text = RollType:CreateFontString("CPlootResultFrameRollTypeText", "ARTWORK", "GameFontNormal") 
	RollType.Text:SetText("Test\nText")
	RollType.Text:SetPoint("CENTER", RollType, "CENTER", 0,0)
	RollType:EnableMouse(true)
	CPlootFrameObj["CPlootResultFrameRollType"] = RollType


	-- Raider colum
	local CPlootResultBackgroundRaider = CreateFrame("Frame", "CPlootResultBackgroundRaider", CPlootFrames:GetFrame("CPlootResultFrame"))
	CPlootResultBackgroundRaider:SetBackdrop(CPlootFrameBGWhite)
	CPlootResultBackgroundRaider:SetBackdropBorderColor(255/255,153/255,0/255,1)
	CPlootResultBackgroundRaider:SetBackdropColor(0/255,0/255,0/255,0.6) 
	CPlootResultBackgroundRaider:SetPoint("TOPLEFT","CPlootResultFrame","TOPLEFT", 32, -85)
	CPlootResultBackgroundRaider:SetPoint("BOTTOMLEFT","CPlootResultFrame","BOTTOMLEFT", -28, 20)
	local CPlootResultBackgroundRaiderWidth = (CPlootFrameObj["CPlootResultFrame"]:GetWidth() -60) / 3
	CPlootResultBackgroundRaider:SetWidth(167)
	CPlootResultBackgroundRaider:EnableMouse(true)
	CPlootFrameObj["CPlootResultBackgroundRaider"] = CPlootResultBackgroundRaider

	-- Raider Title
	local CPlootResultRaiderTitle = CreateFrame("Frame", "CPlootResultRaiderTitle", CPlootResultBackgroundRaider)
	CPlootResultRaiderTitle:SetBackdrop(CPlootFrameTitle)
	CPlootResultRaiderTitle:SetBackdropColor(1,1,1,0.2)
	CPlootResultRaiderTitle:SetPoint("TOPLEFT", "CPlootResultBackgroundRaider", "TOPLEFT", 5, -5)
	CPlootResultRaiderTitle:SetPoint("TOPRIGHT", "CPlootResultBackgroundRaider", "TOPRIGHT", -5, 5)
	CPlootResultRaiderTitle.Text = CPlootResultRaiderTitle:CreateFontString("CPlootResultRaiderTitleText", "ARTWORK", "GameFontNormal")
	CPlootResultRaiderTitle.Text:SetText("Raiders")
	CPlootResultRaiderTitle.Text:SetPoint("CENTER", CPlootResultRaiderTitle, "CENTER", 0,0)
	CPlootResultRaiderTitle:SetHeight(CPlootResultRaiderTitle.Text:GetHeight() + 31)
	CPlootFrameObj["CPlootResultRaiderTitle"] = CPlootResultRaiderTitle

	-- Member colum
	local CPlootResultBackgroundMember = CreateFrame("Frame", "CPlootResultBackgroundMember", CPlootFrames:GetFrame("CPlootResultFrame"))
	CPlootResultBackgroundMember:SetBackdrop(CPlootFrameBGWhite)
	CPlootResultBackgroundMember:SetBackdropBorderColor(255/255,153/255,0/255,1)
	CPlootResultBackgroundMember:SetBackdropColor(0/255,0/255,0/255,0.6) 
	CPlootResultBackgroundMember:SetPoint("TOPLEFT","CPlootResultBackgroundRaider","TOPRIGHT", -3, 0)  -- Minus 2.5 pr windows
	CPlootResultBackgroundMember:SetPoint("BOTTOMLEFT","CPlootResultBackgroundRaider","BOTTOMRIGHT", -3, 0)  -- Minus 2.5 pr windows
	CPlootResultBackgroundMember:SetWidth(CPlootResultBackgroundRaider:GetWidth())
	CPlootResultBackgroundMember:EnableMouse(true)
	CPlootFrameObj["CPlootResultBackgroundRaider"] = CPlootResultBackgroundMember

	-- Member Title
	local CPlootResultMemberTitle = CreateFrame("Frame", "CPlootResultMemberTitle", CPlootResultBackgroundMember)
	CPlootResultMemberTitle:SetBackdrop(CPlootFrameTitle)
	CPlootResultMemberTitle:SetBackdropColor(1,1,1,0.2)
	CPlootResultMemberTitle:SetPoint("TOPLEFT", "CPlootResultBackgroundMember", "TOPLEFT", 5, -5)
	CPlootResultMemberTitle:SetPoint("TOPRIGHT", "CPlootResultBackgroundMember", "TOPRIGHT", -5, 5)
	CPlootResultMemberTitle.Text = CPlootResultMemberTitle:CreateFontString("CPlootResultRaiderTitleText", "ARTWORK", "GameFontNormal")
	CPlootResultMemberTitle.Text:SetText("Members")
	CPlootResultMemberTitle.Text:SetPoint("CENTER", CPlootResultMemberTitle, "CENTER", 0,0)
	CPlootResultMemberTitle:SetHeight(CPlootResultMemberTitle.Text:GetHeight() + 31)
	CPlootFrameObj["CPlootResultMemberTitle"] = CPlootResultMemberTitle

	-- Unknown colum
	local CPlootResultBackgroundUnknown = CreateFrame("Frame", "CPlootResultBackgroundUnknown", CPlootFrames:GetFrame("CPlootResultFrame"))
	CPlootResultBackgroundUnknown:SetBackdrop(CPlootFrameBGWhite)
	CPlootResultBackgroundUnknown:SetBackdropBorderColor(255/255,153/255,0/255,1)
	CPlootResultBackgroundUnknown:SetBackdropColor(0/255,0/255,0/255,0.6) 
	CPlootResultBackgroundUnknown:SetPoint("TOPLEFT","CPlootResultBackgroundMember","TOPRIGHT", -3, 0)
	CPlootResultBackgroundUnknown:SetPoint("BOTTOMLEFT","CPlootResultBackgroundMember","BOTTOMLEFT", -3, 0)
	CPlootResultBackgroundUnknown:SetWidth(CPlootResultBackgroundRaider:GetWidth())
	CPlootResultBackgroundUnknown:EnableMouse(true)
	CPlootFrameObj["CPlootResultBackgroundUnknown"] = CPlootResultBackgroundUnknown

	-- Unknown Title
	local CPlootResultUnknownTitle = CreateFrame("Frame", "CPlootResultUnknownTitle", CPlootResultBackgroundUnknown)
	CPlootResultUnknownTitle:SetBackdrop(CPlootFrameTitle)
	CPlootResultUnknownTitle:SetBackdropColor(1,1,1,0.2)
	CPlootResultUnknownTitle:SetPoint("TOPLEFT", "CPlootResultBackgroundUnknown", "TOPLEFT", 5, -5)
	CPlootResultUnknownTitle:SetPoint("TOPRIGHT", "CPlootResultBackgroundUnknown", "TOPRIGHT", -5, 5)
	CPlootResultUnknownTitle.Text = CPlootResultUnknownTitle:CreateFontString("CPlootResultUnknownTitleText", "ARTWORK", "GameFontNormal")
	CPlootResultUnknownTitle.Text:SetText("Unknown")
	CPlootResultUnknownTitle.Text:SetPoint("CENTER", CPlootResultUnknownTitle, "CENTER", 0,0)
	CPlootResultUnknownTitle:SetHeight(CPlootResultUnknownTitle.Text:GetHeight() + 31)
	CPlootFrameObj["CPlootResultUnknownTitle"] = CPlootResultUnknownTitle


end

--------------------------------------------------------
----   Clean up CPlootFrame when it is closed      ----
--------------------------------------------------------
function CPlootFrames:CleanupCPlootFrame()
	for Name,_ in pairs(CPlootFrameObj) do
		if (string.find(Name, "CPlootTextBox%d+")) then
			local Frame = CPlootFrames:GetFrame(Name)
			Frame.Text:SetText("You should never see this")
			Frame:SetScript("OnEnter", function()  end)
			Frame:SetScript("OnLeave", function()  end)
			Frame:Hide()
			CPlootFrameObj[Name] = Frame
		elseif (string.find(Name, "CPlootMainSpec%d+")) then
			local Button = CPlootFrames:GetFrame(Name)
			Button:SetText("Unused")
			Button:SetScript("OnMouseUP", function() CPloot:Print("This should never happen. Tell Presco he has an unused button in circulation") end)
			Button:Hide()
			Button["LootInfo"] = nil
			CPlootFrameObj[Name] = Button
		elseif (string.find(Name, "CPlootOffSpec%d+")) then
			local Button = CPlootFrames:GetFrame(Name)
			Button:SetText("Unused")
			Button:SetScript("OnMouseUP", function() CPloot:Print("This should never happen. Tell Presco he has an unused button in circulation") end)
			Button:Hide()
			Button["LootInfo"] = nil
			CPlootFrameObj[Name] = Button
		end
	end
	CPlootFramesRuntime["CPlootTextBoxWidth"] = nil
end

--------------------------------------------------------
----   Reset the Result player frames pr player     ----
--------------------------------------------------------
function CPlootFrames:CleanupResultFrame()
	for Name,_ in pairs(CPlootFrameObj) do
		if (string.find(Name, "CPlootResultPlayerFrame%d+")) then
			local Frame = CPlootFrames:GetFrame(Name)
			Frame:Hide()
			Frame.Time.Text:SetText("")
			Frame.Roll.Text:SetText("")
			Frame.Name.Text:SetText("")
			Frame.Name["Player"] = nil
		end
	end
	CPlootFrameObj["CPlootResultFrame"]:SetHeight(CPlootFramesRuntime["CPlootResultFrameBaseHeight"])
	CPlootFrameObj["CPlootResultFrame"]:SetWidth(393)
	CPlootFrameObj["CPlootResultBackgroundUnknown"]:Hide()
	local CPlootRuntime = CPloot:GetRuntime()
	CPlootRuntime["RollStatus"] = "Stopped"
end

--------------------------------------------------------
----    Get the Result player frames pr player      ----
--------------------------------------------------------
function CPlootFrames:GetResultPlayerFrame(Index)

	local Frame = CPlootFrames:GetFrame("CPlootResultPlayerFrame" .. Index)
	if (not Frame) then
		-- Create Text frame for itemlink text
		local CPlootFrameBG = {
			tile = true,
			tileSize = 8,
			edgeSize = 12,
			insets = {left = 2, right = 2, top = 2, bottom = 2},
--			bgFile = "Interface/DialogFrame/UI-DialogBox-Background",
			bgFile = "Interface/Tooltips/UI-Tooltip-Background",
			edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
		}

		Frame = CreateFrame("Frame", "CPlootResultPlayerFrame" .. Index, CPlootFrames:GetFrame("CPlootResultBackgroundRaider"))
		Frame:SetBackdrop(CPlootFrameBG)
		Frame:SetBackdropColor(1,1,1,0.5) 
		Frame:SetBackdropBorderColor(35,135,70,1)
		Frame:EnableMouse(true)
		Frame:SetID(Index)
		Frame:SetScript("OnMouseDown", function(...) CPlootFrames:CPlootResultPlayerDDFrameClicked(...) end)
		Frame:SetScript("OnMouseUP", function()  end)

		-- Roll
		local Roll = CreateFrame("Frame", "CPlootResultPlayerRoll" .. Index, Frame)
		Roll:SetPoint("LEFT", Frame, "LEFT", 0, 0)
		Roll.Text = Roll:CreateFontString("CPlootResultPlayerRollText" .. Index, "ARTWORK", "GameFontNormalSmall")
		Roll.Text:SetPoint("Center", Roll, "Center", 0, 0)
		Roll.Text:SetJustifyH("LEFT")
		Roll:SetBackdrop(CPlootFrameBG)
		Roll:SetBackdropColor(0,0,0,0)
		Roll:SetBackdropBorderColor(0,0,0,0)
		Frame.Roll = Roll

		-- Time Stamp Text Frame
		local Time = CreateFrame("Frame", "CPlootResultPlayerTime" .. Index, Frame)
		Time:SetPoint("RIGHT", Frame, "RIGHT", 0, 0)
		Time.Text = Time:CreateFontString("CPlootResultPlayerTimeText" .. Index, "ARTWORK", "GameFontNormalSmall")
		Time.Text:SetPoint("Center", Time, "Center", 0, 0)
		Time.Text:SetJustifyH("RIGHT")
		Time:SetBackdrop(CPlootFrameBG)
		Time:SetBackdropColor(0,0,0,0)
		Time:SetBackdropBorderColor(0,0,0,0)
		Frame.Time = Time

		-- Name Text Frame
		local Name = CreateFrame("Frame", "CPlootResultPlayerName" .. Index, Frame)
		Name:SetPoint("TOPLEFT", Roll, "TOPRIGHT", -3, 0)
		Name:SetPoint("TOPRIGHT", Time, "TOPLEFT", 3, 0)
		Name:SetPoint("BOTTOMLEFT", Roll, "BOTTOMRIGHT", -3, 0)
		Name:SetPoint("BOTTOMRIGHT", Time, "BOTTOMLEFT", 3, 0)
		Name.Text = Name:CreateFontString("CPlootResultPlayerNameText" .. Index, "ARTWORK", "GameFontNormal")
		Name.Text:SetPoint("Center", Name, "Center", 0, 0)
		Name.Text:SetJustifyH("CENTER")
		Name:SetBackdrop(CPlootFrameBG)
		Name:SetBackdropColor(0,0,0,0)
		Name:SetBackdropBorderColor(0,0,0,0)
		Frame.Name = Name

		CPlootFrameObj["CPlootResultPlayerFrame" .. Index] = Frame
	end
	return Frame
end

--------------------------------------------------------
----          Update Result window                   ---
--------------------------------------------------------
-- TODO change sorting to match the new Index type.
function CPlootFrames:PopulateResultWindow(CPlootCurrentRolls)

	local CPlootRaiderFrameNumber = 0  -- used to check if it's the first frame, and decide the height of the CPlootResult window
	local CPlootMemberFrameNumber = 0  
	local CPlootUnknownFrameNumber = 0
	local LastRaiderFrame = "fubar"  -- Keep track of the last frame used, so I can attach it to the right place
	local LastMemberFrame = "fubar"
	local LastUnknownFrame = "fubar"

	for i=1, #CPlootCurrentRolls do
		Frame = CPlootFrames:GetResultPlayerFrame(i)   -- Get the Frame
		if (string.sub(CPlootCurrentRolls[i]["Index"],1,1) == "1") then -- This is a raider
			CPlootRaiderFrameNumber = CPlootRaiderFrameNumber + 1  -- Increment Frame Number for calculating Height and SetPoint
			if (CPlootRaiderFrameNumber == 1) then
				Frame:SetPoint("TOPLEFT", CPlootFrames:GetFrame("CPlootResultRaiderTitle"), "BOTTOMLEFT", 0, -5)
				Frame:SetPoint("TOPRIGHT", CPlootFrames:GetFrame("CPlootResultRaiderTitle"), "BOTTOMRIGHT", 0, -5)
				LastRaiderFrame = "CPlootResultPlayerFrame" .. i
			else
				Frame:SetPoint("TOPLEFT", CPlootFrames:GetFrame(LastRaiderFrame), "BOTTOMLEFT", 0, 2)
				Frame:SetPoint("TOPRIGHT", CPlootFrames:GetFrame(LastRaiderFrame), "BOTTOMRIGHT", 0, 2)
				LastRaiderFrame = "CPlootResultPlayerFrame" .. i
			end
		elseif (string.sub(CPlootCurrentRolls[i]["Index"],1,1) == "2") then -- This is a member / Initiates
			CPlootMemberFrameNumber = CPlootMemberFrameNumber + 1
			if (CPlootMemberFrameNumber == 1) then
				Frame:SetPoint("TOPLEFT", CPlootFrames:GetFrame("CPlootResultMemberTitle"), "BOTTOMLEFT", 0, -5)
				Frame:SetPoint("TOPRIGHT", CPlootFrames:GetFrame("CPlootResultMemberTitle"), "BOTTOMRIGHT", 0, -5)
				LastMemberFrame = "CPlootResultPlayerFrame" .. i
			else
				Frame:SetPoint("TOPLEFT", CPlootFrames:GetFrame(LastMemberFrame), "BOTTOMLEFT", 0, 2)
				Frame:SetPoint("TOPRIGHT", CPlootFrames:GetFrame(LastMemberFrame), "BOTTOMRIGHT", 0, 2)
				LastMemberFrame = "CPlootResultPlayerFrame" .. i
			end
		elseif (string.sub(CPlootCurrentRolls[i]["Index"],1,1) == "3") then -- This is not a guild member
			CPlootUnknownFrameNumber = CPlootUnknownFrameNumber + 1
			if (CPlootUnknownFrameNumber == 1) then
				Frame:SetPoint("TOPLEFT", CPlootFrames:GetFrame("CPlootResultUnknownTitle"), "BOTTOMLEFT", 0, -5)
				Frame:SetPoint("TOPRIGHT", CPlootFrames:GetFrame("CPlootResultUnknownTitle"), "BOTTOMRIGHT", 0, -5)
				LastUnknownFrame = "CPlootResultPlayerFrame" .. i
			else
				Frame:SetPoint("TOPLEFT", CPlootFrames:GetFrame(LastUnknownFrame), "BOTTOMLEFT", 0, 2)
				Frame:SetPoint("TOPRIGHT", CPlootFrames:GetFrame(LastUnknownFrame), "BOTTOMRIGHT", 0, 2)
				LastUnknownFrame = "CPlootResultPlayerFrame" .. i
			end

		end

		-- Set the background color according to if the armortype match or not.
		if (CPlootCurrentRolls[i]["ArmorTypeMatch"] == "8") then
			Frame:SetBackdropColor(0,255/255,0,0.4)
		else
			Frame:SetBackdropColor(0,0,0,0.1)
		end
		Frame:SetBackdropBorderColor(1,1,1,0.4)

		-- Set the position and text of the 2 text frames
		Frame.Roll.Text:SetText(CPlootCurrentRolls[i]["Roll"])
		local COLOR = CPloot:GetTextColor("Name", CPlootCurrentRolls[i]["Name"])
		Frame.Name.Text:SetText(COLOR .. CPlootCurrentRolls[i]["Name"])
		Frame.Name["Player"] = CPlootCurrentRolls[i]["Name"]

		if (CPlootCurrentRolls[i]["WinnerListNumber"] == "000") then
			Frame.Time.Text:SetText("")
		else
			Frame.Time.Text:SetText(CPlootCurrentRolls[i]["WinnerListNumber"])
		end
		
		Frame:SetHeight(Frame.Name.Text:GetHeight() +16)
		Frame.Roll:SetHeight(Frame:GetHeight())
		Frame.Roll:SetWidth(30)
		Frame.Name:SetHeight(Frame:GetHeight())
		Frame.Time:SetWidth(25)
		Frame.Time:SetHeight(Frame:GetHeight())

		-- Find the height of the CPlootResultFrame
		local CPlootResultHeightBase = CPlootFramesRuntime["CPlootResultFrameBaseHeight"]
		local DecidingFrame = 0
		if ( (CPlootRaiderFrameNumber > CPlootMemberFrameNumber) or (CPlootRaiderFrameNumber > CPlootUnknownFrameNumber) ) then
			DecidingFrame = CPlootRaiderFrameNumber
		end

		if ( CPlootMemberFrameNumber > DecidingFrame ) then
			DecidingFrame = CPlootMemberFrameNumber
		end

		if ( CPlootUnknownFrameNumber > DecidingFrame ) then
			DecidingFrame = CPlootUnknownFrameNumber
		end

		CPlootFrameObj["CPlootResultFrame"]:SetHeight(CPlootResultHeightBase + (DecidingFrame * (Frame:GetHeight() + 1) ) )

		-- Adjust the width to match if the unkown colum is shown or not.
		if (CPlootUnknownFrameNumber > 0) then
			CPlootFrameObj["CPlootResultFrame"]:SetWidth(560)
			CPlootFrameObj["CPlootResultBackgroundUnknown"]:Show()
		else
			CPlootFrameObj["CPlootResultFrame"]:SetWidth(394)
			CPlootFrameObj["CPlootResultBackgroundUnknown"]:Hide()
		end

		Frame:Show()
	end

end

--------------------------------------------------------
----      Create the player dropdown frame          ----
--------------------------------------------------------
function CPlootFrames:CreateResultPlayerReasignDDFrame()
	local Frame = CreateFrame("Frame", "CPlootResultPlayerDDFrame", UIParent, "UIDropDownMenuTemplate")
	Frame:SetFrameStrata("FULLSCREEN_DIALOG")
	CPlootFrameObj["CPlootResultPlayerDDFrame"] = Frame
end

--------------------------------------------------------
----      Generate Player Dropdown frame layout     ----
--------------------------------------------------------
function CPlootInitPlayerDDFrame(Level, arg1, Name)

	if (UIDROPDOWNMENU_MENU_VALUE == nil) then
		-- Cleanup from previous dropdown display
		DropDownTable.func = nil
		DropDownTable.checked = nil
		DropDownTable.value = nil
		DropDownTable.hasArrow = nil

		if (Name) then -- Workaround as this is initialized at startup and I don't have a name.
			DropDownTable.text = "Award to " .. CPloot:GetTextColor("Name",Name) .. Name
		else
			DropDownTable.text = "Award Item"
		end

		DropDownTable.isTitle = nil
		DropDownTable.hasArrow = nil
		DropDownTable.value = nil
		function DropDownTable.func()
			CPloot:AwardItem(Name)
		end
		UIDropDownMenu_AddButton(DropDownTable)

	end

end

--------------------------------------------------------
----  Show the popup menu when a player is clicked  ----
----  on in CPlootResultPlayerFrame                ----       
--------------------------------------------------------
function CPlootFrames:CPlootResultPlayerDDFrameClicked(this)
	local Frame = CPlootFrames:GetFrame("CPlootResultPlayerDDFrame")
	ToggleDropDownMenu(1, nil, Frame, this, this:GetWidth() , this:GetHeight(), this.Name["Player"])
end


--------------------------------------------------------
----   Populate the ML popup choices at raid setup  ----
--------------------------------------------------------
function CPlootFrames:PopulateMLChoiseFrame()
	local CPlootRuntime = CPloot:GetRuntime()
	local MLCandidates = CPlootRuntime["MLCandidates"]
	local Index = 1
	for Foo,Name in pairs(MLCandidates) do
		CPlootFrames:CreateMLChoiseNameButton(Index,Name)
		Index = Index + 1
	end
--	_G["InterfaceOptionsFrame"]:Hide()
--	_G["GameMenuFrame"]:Hide()
	CPlootFrameObj["CPlootChooseML"]:Show()
end

--------------------------------------------------------
----     Create the buttons for choosing the ML     ----
--------------------------------------------------------
function CPlootFrames:CreateMLChoiseNameButton(Index,Name)
	if (Index < 11) then
		
		local MLChoiseButton = CreateFrame("Button", "CPlootMLChoiseButton" .. Index, CPlootFrames:GetFrame("CPlootChooseML"), "UIPanelButtonTemplate")
--		MLChoiseButton:SetPoint("TOPLEFT", "CPlootChooseML", "TOPLEFT", 30, -25)
		MLChoiseButton:SetText(Name)
		MLChoiseButton:SetWidth(135)
		MLChoiseButton:SetHeight(25)
		MLChoiseButton:SetScript("OnMouseUp", function() CPloot_ConfigDb["MasterLooter"] = CPloot:FormatName(Name); CPloot:RaidSetup(); CPlootFrameObj["CPlootChooseML"]:Hide() end)
		MLChoiseButton:SetScript("OnMouseDown", function()  end)
		CPlootFrameObj["CPlootMLChoiseButton" .. Index] = MLChoiseButton

		if (Index == 1) then
			MLChoiseButton:SetPoint("TOPLEFT", "CPlootChooseML", "TOPLEFT", 13.5, -25)
		else
			local Offset = Index * 25
			MLChoiseButton:SetPoint("TOPLEFT", "CPlootChooseML", "TOPLEFT", 13.5, -Offset)
		end
	end
	CPlootChooseML = CPlootFrames:GetFrame("CPlootChooseML")
	local BaseHeight = 65
	local Multiplier = Index - 1
	local Height = BaseHeight + ( 25 * Multiplier)
	CPlootChooseML:SetHeight(Height)

end

----------------------------------
-- Used to get the frame class ---
----------------------------------
function CPloot:NewCPlootFrames()
   return CPlootFrames
end

----------------------------
--- Get the Frame object ---
----------------------------
function CPlootFrames:GetFrame(name)
   return CPlootFrameObj[name]
end

